package cc.vv.btpartyjob.jnimodule.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import cc.vv.baselibrary.util.LKToastUtil;
import cc.vv.btpartyjob.jnimodule.R;
import cc.vv.btpartyjob.jnimodule.moudel.bean.tree.Node;
import cc.vv.btpartyjob.jnimodule.moudel.bean.tree.TreeListViewAdapter;

/**
 * 组织架构
 * Created by Chen.qi on 2018/11/2.
 */

public class SimpleTreeAdapter extends TreeListViewAdapter {
    private List<Node> mdatas;
    private Context mContext;

    /**
     * 选择类型
     */
    private int chooseType;

    public SimpleTreeAdapter(ListView mTree, Context context, List<Node> datas, int defaultExpandLevel, int chooseType) throws IllegalArgumentException, IllegalAccessException {
        super(mTree, context, datas, defaultExpandLevel);
        mdatas = datas;
        mContext = context;
        this.chooseType = chooseType;
    }

    @Override
    public View getConvertView(Node node, int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.adapter_tree_item, null);
            holder = new ViewHolder();
            holder.icon = convertView
                    .findViewById(R.id.iv_treenode_icon);
            holder.iv_click = convertView.findViewById(R.id.iv_click);
            holder.label = convertView
                    .findViewById(R.id.tv_treenode_label);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (node.getZtreeNodesBean().isCheck()) {
            holder.iv_click.setSelected(true);
        } else {
            holder.iv_click.setSelected(false);
        }

        if (chooseType == 0) {
            holder.iv_click.setVisibility(View.VISIBLE);

        } else {
            if ((chooseType + "").equals(node.getZtreeNodesBean().getType())) {
                holder.iv_click.setVisibility(View.VISIBLE);
            } else {
                holder.iv_click.setVisibility(View.INVISIBLE);
            }
        }
        setclick(holder.iv_click, node);
        if (node.getIcon() == -1) {
            if (node.isLeaf()) {
                holder.icon.setImageResource(R.mipmap.icon_branch_flag);
            } else {
                holder.icon.setImageResource(R.mipmap.icon_add);
            }
        } else {
            holder.icon.setImageResource(node.getIcon());
        }
        holder.label.setText(node.getZtreeNodesBean().getName());
        return convertView;
    }

    private final class ViewHolder {
        ImageView icon;
        ImageView iv_click;
        TextView label;
    }


    /**
     * 设置点击事件
     *
     * @param chose
     */
    private void setclick(final ImageView chose, final Node node) {
        chose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (chooseType == 0) {
                    //都可选择
                    for (int i = 0; i < mdatas.size(); i++) {
                        if (node.getZtreeNodesBean().getId().equals(mdatas.get(i).getId())) {
                            mdatas.get(i).getZtreeNodesBean().setCheck(true);
                        } else {
                            mdatas.get(i).getZtreeNodesBean().setCheck(false);
                        }
                    }
                } else if (chooseType == 1) {
                    //只可选则街道
                    for (int i = 0; i < mdatas.size(); i++) {
                        if (node.getZtreeNodesBean().getType().equals("1")) {
                            if (node.getZtreeNodesBean().getId().equals(mdatas.get(i).getId())) {
                                mdatas.get(i).getZtreeNodesBean().setCheck(true);
                            } else {
                                mdatas.get(i).getZtreeNodesBean().setCheck(false);
                            }
                        } else {
                            LKToastUtil.showToastShort("只可选择街道");
                            break;
                        }
                    }
                } else if (chooseType == 2) {
                    //只可选则社区
                    for (int i = 0; i < mdatas.size(); i++) {
                        if (node.getZtreeNodesBean().getType().equals("2")) {
                            if (node.getZtreeNodesBean().getId().equals(mdatas.get(i).getId())) {
                                mdatas.get(i).getZtreeNodesBean().setCheck(true);
                            } else {
                                mdatas.get(i).getZtreeNodesBean().setCheck(false);
                            }
                        } else {
                            LKToastUtil.showToastShort("只可选择社区");
                            break;
                        }
                    }
                } else if (chooseType == 3) {
                    //只可选则社区
                    for (int i = 0; i < mdatas.size(); i++) {
                        if (node.getZtreeNodesBean().getType().equals("3")) {
                            if (node.getZtreeNodesBean().getId().equals(mdatas.get(i).getId())) {
                                mdatas.get(i).getZtreeNodesBean().setCheck(true);
                            } else {
                                mdatas.get(i).getZtreeNodesBean().setCheck(false);
                            }
                        } else {
                            LKToastUtil.showToastShort("只可选择网格");
                            break;
                        }
                    }
                } else if (chooseType == 4) {
                    //只可选则单位
                    for (int i = 0; i < mdatas.size(); i++) {
                        if (node.getZtreeNodesBean().getType().equals("4")) {
                            if (node.getZtreeNodesBean().getId().equals(mdatas.get(i).getId())) {
                                mdatas.get(i).getZtreeNodesBean().setCheck(true);
                            } else {
                                mdatas.get(i).getZtreeNodesBean().setCheck(false);
                            }
                        } else {
                            LKToastUtil.showToastShort("只可选则单位");
                            break;
                        }
                    }
                } else if (chooseType == 6) {
                    //只可选择楼栋
                    for (int i = 0; i < mdatas.size(); i++) {
                        if (node.getZtreeNodesBean().getType().equals("6")) {
                            if (node.getZtreeNodesBean().getId().equals(mdatas.get(i).getId())) {
                                mdatas.get(i).getZtreeNodesBean().setCheck(true);
                            } else {
                                mdatas.get(i).getZtreeNodesBean().setCheck(false);
                            }
                        } else {
                            LKToastUtil.showToastShort("只可选择楼栋");
                            break;
                        }
                    }
                } else if (chooseType == 8) {
                    //只可选择单元中心户
                    for (int i = 0; i < mdatas.size(); i++) {
                        if (node.getZtreeNodesBean().getType().equals("8")) {
                            if (node.getZtreeNodesBean().getId().equals(mdatas.get(i).getId())) {
                                mdatas.get(i).getZtreeNodesBean().setCheck(true);
                            } else {
                                mdatas.get(i).getZtreeNodesBean().setCheck(false);
                            }
                        } else {
                            LKToastUtil.showToastShort("只可选择单元中心户");
                            break;
                        }
                    }
                }

                notifyData();
            }
        });
    }


    /**
     * 刷新数据
     */
    private void notifyData() {
        notifyDataSetChanged();
    }


}
